//
// Created by dave on 18-8-5.
//

#pragma once

#include <atomic>
#include <memory>
#include <string>
#include <evpp/event_loop.h>
#include <evpp/tcp_server.h>
#include <evpp/buffer.h>
#include <evpp/tcp_conn.h>

class Jtt808Server {
public:
    Jtt808Server(const std::string& listen_addr/*ip:port*/,
                 const std::string& name,
                 uint32_t acceptThreadNum, uint32_t childThreadNum);

    virtual void start();

private:
    virtual void onConnection(const evpp::TCPConnPtr& conn);

    virtual void onMessage(const evpp::TCPConnPtr& conn, evpp::Buffer* msg);

    virtual void onFrame(const evpp::TCPConnPtr &conn, const char* ptr, size_t length);

private:
    clock_t startClock_ = clock();
    std::atomic_int msgCounter_{0};
    std::unique_ptr<evpp::TCPServer> tcpServer_;
    std::shared_ptr<evpp::EventLoop> acceptLoop_;

    std::shared_ptr<evpp::EventLoop> childLoop_;
    std::shared_ptr<evpp::EventLoopThreadPool> childPool_;
};


